package com.magina.project.entity;

import org.hibernate.annotations.GenericGenerator;

import javax.persistence.Column;
import javax.persistence.GeneratedValue;
import javax.persistence.MappedSuperclass;

@MappedSuperclass
public abstract class AbstractIdEntity implements  java.io.Serializable  {

    private static final long serialVersionUID = 1L;

    private String id;

    /**
     *     1:   默认hibernate uuid生成策略
     *     @GenericGenerator(name="idGenerator", strategy="uuid") //这个是hibernate的注解/生成32位UUID
     *     @GeneratedValue(generator="idGenerator")
     *     2:   自定义主键生成策略
     *     @GeneratedValue(generator="selfUUIDGenerator")
     *     @GenericGenerator(name = "selfUUIDGenerator", strategy = "com.magina.project.entity.SelfUUIDGenerator")
     *     3:   自增策略
     *     @GeneratedValue(strategy = GenerationType.IDENTITY)//自增id

     */
    @javax.persistence.Id
    @Column(name = "id",nullable=false,length=32)
    @GenericGenerator(name="idGenerator", strategy="uuid")
    @GeneratedValue(generator="idGenerator")
    public String getId() {
        return id;
    }

    public void setId(String id) {
        this.id = id;
    }

    @Override
    public boolean equals(Object o) {
        if (this == o) {
            return true;
        }
        if (o == null || getClass() != o.getClass()) {
            return false;
        }

        AbstractIdEntity abstractIdEntity = (AbstractIdEntity) o;

        return id != null ? id.equals(abstractIdEntity.id) : abstractIdEntity.id == null;
    }

    @Override
    public int hashCode() {
        return id != null ? id.hashCode() : 0;
    }
}